package com.leoman.service.impl;

import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.dao.VideoResumeDao;
import com.leoman.entity.VideoResume;
import com.leoman.service.VideoResumeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * VideoResumeServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:26.
 */
@Service
public class VideoResumeServiceImpl implements VideoResumeService {

    @Autowired
    private VideoResumeDao videoResumeDao;

    @Override
    public List<VideoResume> findAll() {
        return videoResumeDao.findAll();
    }

    @Override
    public Page<VideoResume> find(int pageNum, int pageSize) {
        return videoResumeDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<VideoResume> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public VideoResume getById(int id) {
        return videoResumeDao.findOne(id);
    }

    @Override
    public void deleteById(int id) {
        videoResumeDao.delete(id);
    }

    @Override
    public VideoResume create(VideoResume videoResume) {
        return videoResumeDao.save(videoResume);
    }

    @Override
    public VideoResume update(VideoResume videoResume) {
        return videoResumeDao.save(videoResume);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<VideoResume> pageByParams(final Integer companyUserId,
                                          final String userName,
                                          final String schoolName,
                                          final String disciplineInfo,
                                          final Integer videoId,
                                          final String educationInfo,
                                          Integer pageNum,
                                          Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);

        return videoResumeDao.findAll(new Specification<VideoResume>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<VideoResume> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (null != companyUserId) {
                    Predicate pre = cb.equal(root.get("videoRecord").get("video").get("companyUser").get("id").as(Integer.class), companyUserId);
                    predicateList.add(pre);
                }

                if (StringUtils.isNotBlank(userName)) {
                    Predicate pre = cb.like(root.get("userResume").get("userName").as(String.class), "%" + userName + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotBlank(schoolName)) {
                    Predicate pre = cb.like(root.get("userResume").get("schoolName").as(String.class), "%" + schoolName + "%");
                    predicateList.add(pre);
                }

                if (StringUtils.isNotBlank(disciplineInfo)) {
                    Predicate pre = cb.like(root.get("userResume").get("disciplineInfo").as(String.class), "%" + disciplineInfo + "%");
                    predicateList.add(pre);
                }

                if (null != videoId) {
                    Predicate pre = cb.equal(root.get("videoRecord").get("video").get("id").as(Integer.class), videoId);
                    predicateList.add(pre);
                }

                if (StringUtils.isNotBlank(educationInfo) && !educationInfo.equals("全部")) {
                    Predicate pre = cb.like(root.get("userResume").get("educationInfo").as(String.class), "%" + educationInfo + "%");
                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "id"));
    }
}